/* $Id:skill_level.cc $ */
/*
   Copyright (C) 2007 by Jurjen Stellingwerff <jurjen@stwerff.xs4all.nl>
   Part of the Moros Project http://moros.sourceforge.net/

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 3.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY.

   See the COPYING file for more details.
*/

#define DEBUG_ROUTINE "skill_level"

#include <sstream>
#include <fstream>
#include "../general/util.h"
#include "../general/log.h"
#include "../general/channel.h"
#include "skill_level.h"
#include "skill.h"
#include "../general/general.h"

skill_level::skill_level() {
    skill=0;
    level=0;
}

// Field : skill

class skill& skill_level::getSkill() const { if (skill==0) throw nullpointer(); return *skill; }
void skill_level::setSkill(class skill &v) { skill=&v; }

// Field : level

const int skill_level::getLevel() const { return level; }
void skill_level::setLevel(const int v) { level=v; }

void skill_level::save(class channel &ch) {
    ch.write("skill_level", "");
    ch.write("s", getSkill().getName());
    ch.write("l", getLevel());
}

void skill_level::load(class channel &ch) {
    if (ch.key=="s" || ch.key=="skill") {
        if (!skill_repo.isSkill(ch.value)) {
            dbg_state("link unknown skill %s", ch.value.c_str());
            class skill rec;
            rec.setName(ch.value);
            skill_repo.setSkill(rec);
        }
        setSkill(skill_repo.getSkill(ch.value));
        ch.next();
    }
    if (ch.key=="l" || ch.key=="level") { setLevel(atoi(ch.value.c_str())); ch.next(); }
}

void skill_level::xml(ofstream &f, int l, int r) {
    f << ncpy(l, "  ") << "<skill_level level=\"" << getLevel() <<
    "\">" << endl;
    if (r<2) try {
        getSkill().xml(f,l+1,r+1);
    } catch (nullpointer n) {}
    f << ncpy(l, "  ") << "</skill_level>" << endl;
}
